using System;
using System.Globalization;

namespace IQIGame.Onigao.Game
{
    [Serializable]
    public struct RectInt
    {
        public static readonly RectInt Empty;

        public int X;

        public int Y;

        public int Width;

        public int Height;

        //public int Left => X;

        //public int Top => Y;

        //public int Right => X + Width;

        //public int Bottom => Y + Height;

        public bool IsEmpty
        {
            get
            {
                if (Height == 0 && Width == 0 && X == 0)
                {
                    return Y == 0;
                }

                return false;
            }
        }

        public RectInt(int x, int y, int width, int height)
        {
            this.X = x;
            this.Y = y;
            this.Width = width;
            this.Height = height;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is RectInt))
            {
                return false;
            }

            RectInt rectangle = (RectInt)obj;
            if (rectangle.X == X && rectangle.Y == Y && rectangle.Width == Width)
            {
                return rectangle.Height == Height;
            }

            return false;
        }

        public static bool operator ==(RectInt left, RectInt right)
        {
            if (left.X == right.X && left.Y == right.Y && left.Width == right.Width)
            {
                return left.Height == right.Height;
            }

            return false;
        }

        public static bool operator !=(RectInt left, RectInt right)
        {
            return !(left == right);
        }

        public bool Contains(int x, int y)
        {
            if (this.X <= x && x <= this.X + this.Width && this.Y <= y)
            {
                return y <= this.Y + this.Height;
            }

            return false;
        }

        public bool Contains(RectInt rect)
        {
            if (X <= rect.X && rect.X + rect.Width <= X + Width && Y <= rect.Y)
            {
                return rect.Y + rect.Height <= Y + Height;
            }

            return false;
        }

        public RectInt ExtendsSize(float ratio)
        {
            return new RectInt(this.X, this.Y, (int)(this.Width * ratio), (int)(this.Height * ratio));
        }

        public override int GetHashCode()
        {
            return X ^ ((Y << 13) | (int)((uint)Y >> 19)) ^ ((Width << 26) | (int)((uint)Width >> 6)) ^ ((Height << 7) | (int)((uint)Height >> 25));
        }

        public void Intersect(RectInt rect)
        {
            RectInt rectangle = Intersect(rect, this);
            X = rectangle.X;
            Y = rectangle.Y;
            Width = rectangle.Width;
            Height = rectangle.Height;
        }

        public static RectInt Intersect(RectInt a, RectInt b)
        {
            int num = Math.Max(a.X, b.X);
            int num2 = Math.Min(a.X + a.Width, b.X + b.Width);
            int num3 = Math.Max(a.Y, b.Y);
            int num4 = Math.Min(a.Y + a.Height, b.Y + b.Height);
            if (num2 >= num && num4 >= num3)
            {
                return new RectInt(num, num3, num2 - num, num4 - num3);
            }

            return Empty;
        }

        public bool IntersectsWith(RectInt rect)
        {
            if (rect.X < X + Width && X < rect.X + rect.Width && rect.Y < Y + Height)
            {
                return Y < rect.Y + rect.Height;
            }

            return false;
        }

        public static RectInt Union(RectInt a, RectInt b)
        {
            int num = Math.Min(a.X, b.X);
            int num2 = Math.Max(a.X + a.Width, b.X + b.Width);
            int num3 = Math.Min(a.Y, b.Y);
            int num4 = Math.Max(a.Y + a.Height, b.Y + b.Height);
            return new RectInt(num, num3, num2 - num, num4 - num3);
        }

        public void Offset(int x, int y)
        {
            X += x;
            Y += y;
        }

        public override string ToString()
        {
            return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) + ",Width=" + Width.ToString(CultureInfo.CurrentCulture) + ",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}";
        }
    }
}
